/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.dahg.project.web;

import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.SessionScoped;
import org.dahg.project.controller.model.Usuario;
import org.dahg.project.controller.model.exceptions.ControllerException;
import org.dahg.project.controller.model.exceptions.ValidationException;
import org.dahg.project.controller.model.services.local.UsuarioService;

/**
 *
 * @author dahg
 */
@ManagedBean
@SessionScoped
public class UsuarioBean extends AbstractWebBean {
    
    @EJB
    private UsuarioService servicio;    
    private Usuario usuario;
    
    @ManagedProperty("#{pedidoBean}")
    private PedidoBean pedidoBean;
    
    private String id;
    private String pass;

    public String validate() {
        try {            
            usuario = servicio.validate(id, pass); 
            return "main";
        } catch (ValidationException ex) {
            addError(ex.getMessage());
        } catch (ControllerException ex) {
            addError(ex.getMessage());
        }
        
        return null;
    }

    public String logout() {
        usuario=null;
        pedidoBean.setPedido(null);
        pedidoBean.init();
        return "login";
    }
    
    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getPass() {
        return pass;
    }

    public void setPass(String pass) {
        this.pass = pass;
    }
    
    public Usuario getUsuario() {
        return usuario;
    }

    public void setPedidoBean(PedidoBean pedidoBean) {
        this.pedidoBean = pedidoBean;
    }
    
}
